﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.DOL;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class EmployeeVacation : BaseUserControl
    {
        private DataSet dsEmployeeVacatioin = new DataSet();

        public EmployeeVacation()
        {
            InitializeComponent();
            initDateTime();
            ButtonItemIconHelper.InitRibbonBar(this.ribbonBar1);
        }

        private void initDateTime()
        {
            dateQueryMonth.Value = DateTime.Now;
            queryDateTime(dateQueryMonth.Value);
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            EmployeeVacationAdd employeeVacationAdd = new EmployeeVacationAdd();

            if (employeeVacationAdd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            queryDateTime(this.dateQueryMonth.Value);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (dateQueryMonth.Value == null)
            {
                MsgBox.MsgInfo("请选择正确的时间。");
                return;
            }
            queryDateTime(dateQueryMonth.Value);
        }

        private void queryDateTime(DateTime dtQuery)
        {
            
            string errorMsg = string.Empty;
            DateTime dtStart = new DateTime(dateQueryMonth.Value.Year, dateQueryMonth.Value.Month, 1, 0, 0, 0);
            int daysinmonth = DateTime.DaysInMonth(dateQueryMonth.Value.Year, dateQueryMonth.Value.Month);
            DateTime dtEnd = new DateTime(dateQueryMonth.Value.Year, dateQueryMonth.Value.Month, daysinmonth, 23, 59, 59);
            string sQueryStartTime = dtStart.ToString();

            string sQueryEndTime = dtEnd.ToString(); ;
            if (BussinessProxyManager.Instance != null)
            {
                if (!BussinessProxyManager.Instance.QueryEmployeeVacationDateTime(sQueryStartTime, sQueryEndTime, out dsEmployeeVacatioin, out errorMsg))
                {
                    MsgBox.MsgInfo(errorMsg);
                    return;
                }
                else
                {
                    grdCtrlEmplyVacation.BeginUpdate();
                    grdCtrlEmplyVacation.DataSource = dsEmployeeVacatioin.Tables[0];
                    grdCtrlEmplyVacation.EndUpdate();
                }
            }

        }

        private DBEntityEmoloyeeVacation getEmployeeVacationInfo(int gridviewRowHandle)
        {
            if (gridviewRowHandle < 0)
            {
                return null;
            }
            DataRow drEmployeeVacation = ((DataRowView)grdViewEmployeeVacation.GetRow(grdViewEmployeeVacation.FocusedRowHandle)).Row;
            DBEntityEmoloyeeVacation employeeVacation = new DBEntityEmoloyeeVacation();
            employeeVacation.EMPLOYEEID = Convert.ToInt32(drEmployeeVacation[DBEntityEmployee.FieldName.EMPLOYEEID]);
            employeeVacation.VSTARTDATETIME = Convert.ToDateTime(drEmployeeVacation[DBEntityEmoloyeeVacation.FieldName.VSTARTDATETIME]);
            employeeVacation.VENDDATETIME = Convert.ToDateTime(drEmployeeVacation[DBEntityEmoloyeeVacation.FieldName.VENDDATETIME]);
            employeeVacation.VSTATE = Convert.ToString(drEmployeeVacation[DBEntityEmoloyeeVacation.FieldName.VSTATE]);
            return employeeVacation;

        }

        private void grdViewEmployeeVacation_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle < 0)
            {
                return;
            }

            if (e.Column.FieldName.Equals(DBEntityEmoloyeeVacation.FieldName.VSTATE))
            {
                if (e.DisplayText.Equals(DBEntityEmoloyeeVacation.ENUM_STATE.VOA))
                {
                    e.DisplayText = DBEntityEmoloyeeVacation.ENUM_STATE_CHINESE.VOA;
                }
                else if (e.DisplayText.Equals(DBEntityEmoloyeeVacation.ENUM_STATE.VOT))
                {
                    e.DisplayText = DBEntityEmoloyeeVacation.ENUM_STATE_CHINESE.VOT;
                }
            }

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (grdViewEmployeeVacation.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的记录。");
                return;
            }
            bool bFlag = MsgBox.MsgQue("确定要删除此休假记录吗？");
            if (bFlag == false)
                return;

            string error = string.Empty;
            DBEntityEmoloyeeVacation employeeVacation = getEmployeeVacationInfo(grdViewEmployeeVacation.FocusedRowHandle);
            if (!BussinessProxyManager.Instance.DelEmployeeVacation(employeeVacation, out error))
            {
                MsgBox.MsgInfo(error);
                return;
            }
            grdViewEmployeeVacation.BeginUpdate();
            grdViewEmployeeVacation.DeleteRow(grdViewEmployeeVacation.FocusedRowHandle);
            grdViewEmployeeVacation.UpdateCurrentRow();
            dsEmployeeVacatioin.AcceptChanges();
            grdViewEmployeeVacation.EndUpdate();
                    
            MsgBox.MsgInfo("删除成功！");
        }

        private void grdViewEmployeeVacation_DoubleClick(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grdViewEmployeeVacation.FocusedRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择正确的记录。");
                return;
            }
            string error = string.Empty;
            DBEntityEmoloyeeVacation employeeVacation = getEmployeeVacationInfo(grdViewEmployeeVacation.FocusedRowHandle);

            EmployeeVacationAdd employeeVacationAdd = new EmployeeVacationAdd(employeeVacation);
            if (employeeVacationAdd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            queryDateTime(this.dateQueryMonth.Value);

        }
    }
}
